package com.wxd.modules.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.annotation.DataFilter;
import com.wxd.modules.function.dao.ChangeEmpDeptDao;
import com.wxd.modules.function.entity.*;
import com.wxd.modules.function.service.*;
import com.wxd.modules.sys.entity.SysDeptEntity;
import com.wxd.modules.sys.entity.SysUserEntity;
import com.wxd.modules.sys.service.SysDeptService;
import com.wxd.utils.Constant;
import com.wxd.utils.PageUtils;
import com.wxd.utils.Query;
import com.wxd.validator.ValidatorUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("changeEmpDeptService")
public class ChangeEmpDeptServiceImpl extends ServiceImpl<ChangeEmpDeptDao, EmpChangeDept> implements ChangeEmpDeptService {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private EmpService empService;
    @Autowired
    private WorkExperienceService workExperienceService;
    @Autowired
    private RewardRecordService rewardRecordService;

    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String dateStart=(String)params.get("dateStart");
        String dateEnd=(String)params.get("dateEnd");
        String deptIdStr= (String)params.get("deptId");
        Long deptIdLong;
        //根据部门ID将部门下的所有人员查找出来
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {
            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            deptIdList.add(deptIdLong);
        }
        IPage<EmpChangeDept> page = this.page(
                new Query<EmpChangeDept>().getPage(params),
                new QueryWrapper<EmpChangeDept>().between(StringUtils.isNotBlank(dateStart),"date",dateStart,dateEnd).in(deptIdList!=null,"dept_id",deptIdList)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        for(EmpChangeDept empChangeDept :page.getRecords())
        {
            EmpEntity empEntity = empService.getById(empChangeDept.getEmpId());
            SysDeptEntity deptEntity = sysDeptService.getById(empChangeDept.getDeptId());
            SysDeptEntity deptAgoEntity = sysDeptService.getById(empChangeDept.getDeptIdAgo());
            empChangeDept.setEmpName(empEntity.getEmpName());
            empChangeDept.setDeptName(deptEntity.getName());
            empChangeDept.setIdCard(empEntity.getIdCard());
            empChangeDept.setDeptNameAgo(deptAgoEntity.getName());
        }
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeEmpty(ChangeDept emp) {
        Long[] empIds=emp.getEmpIds();
        Long deptId=emp.getDeptId();
        List<EmpEntity> empEntities= empService.queryEmp(empIds);
        //用户名
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        //校验类型，更改的信息修改
        for(EmpEntity emps:empEntities)
        {
            ValidatorUtils.validateEntity(emps);
            //记录部门变动的信息
            EmpChangeDept empChangeDept =new EmpChangeDept();
            empChangeDept.setEmpId(emps.getEmpId());
            empChangeDept.setDeptId(deptId);
            empChangeDept.setDeptIdAgo(emps.getDeptId());
            empChangeDept.setOperator(username);
            empChangeDept.setDate(new Date());
            this.save(empChangeDept);
            emps.setDeptId(deptId);
            empService.updateById(emps);
        }
    }

    @Override
    public List<EmpChangeDept> queryChangeEmp(Long empId) {
        List<EmpChangeDept> empChangeDepts = baseMapper.queryChangeEmp(empId);
        for(EmpChangeDept empChangeDept : empChangeDepts)
        {
            SysDeptEntity deptEntity = sysDeptService.getById(empChangeDept.getDeptId());
            SysDeptEntity deptAgoEntity = sysDeptService.getById(empChangeDept.getDeptIdAgo());
            empChangeDept.setDeptName(deptEntity.getName());
            empChangeDept.setDeptNameAgo(deptAgoEntity.getName());
        }
        return empChangeDepts;
    }



    @Override
    @DataFilter(subDept = true,user = false)
    public List<Map> changeDeptStatisticsByMonth(Map<String, Object> params) {
        return  baseMapper.changeDeptStatisticsByMonth(params);
    }
}
